package com.shop.controller.users;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Test.None;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.entity.users.User;
import com.shop.service.user.User_service;
import com.shop.util.EmailVerifyUtil;
import com.shop.util.VerifyCodeUtil;

@Controller
public class UserRegister {

	private static final String NONE = null;
	
	@Autowired
	private User_service userService;
	
	/**
	 * 用户名验证
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unused")
	@ResponseBody
	@RequestMapping("/usernameVerify")
	public String usernameVerify(HttpServletRequest request){
		String username = request.getParameter("username");
		User user = userService.qreayUserByName(username);
		Map<String,Boolean> verifyM = (Map) request.getSession().getAttribute("registerVerify");
		//判断用户名是否存在
		if(user!=null){//存在，不可以注册
			verifyM.put("username", false);
			request.getSession().setAttribute("registerVerify",verifyM);
			return "1";
		}
		if (username.length()<6) {//用户名必须超过6位
			verifyM.put("username", false);
			request.getSession().setAttribute("registerVerify",verifyM);
			return "2";
		}
		verifyM.put("username", true);
		request.getSession().setAttribute("registerVerify",verifyM);
		return "0";
	}
	
	/**
	 * 密码验证
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/password1Verify")
	public String password1Verify(HttpServletRequest request){
		String password = request.getParameter("password1");
		Map<String,Boolean> verifyM = (Map) request.getSession().getAttribute("registerVerify");
		//通过大小写字母和数字验证密码安全级别
		if(password.length()<6){
			verifyM.put("password1", false);
			request.getSession().setAttribute("registerVerify",verifyM);
			return "0";
		}
		int da=0;
		int xiao=0;
		int su=0;
		for (int i = 0; i < password.length(); i++) {
			if(password.charAt(i)>=65&&password.charAt(i)<=90){
				da++;
			}
			if(password.charAt(i)>=97&&password.charAt(i)<=122){
				xiao++;
			}
			if(password.charAt(i)>=48&&password.charAt(i)<=57){
				su++;
			}
		}
		verifyM.put("password1", true);
		request.getSession().setAttribute("registerVerify",verifyM);
		
		if(da>0&&xiao>0&&su>0){
			return "3";
		}else if((da>0&&xiao>0)||(su>0&&xiao>0)||(da>0&&su>0)){
			return "2";
		}else {
			return "1";
		}
	}
	
	/**
	 * 判断第一次输入密码和第二次是否一直
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/password2Verify")
	public void password2Verify(HttpServletRequest request){
		String password2 = request.getParameter("password2");
		Map<String,Boolean> verifyM = (Map) request.getSession().getAttribute("registerVerify");
		if(password2.equals("0")){
			verifyM.put("password2", true);
			request.getSession().setAttribute("registerVerify",verifyM);
		}else {
			verifyM.put("password2", false);
			request.getSession().setAttribute("registerVerify",verifyM);
		}
		
	}
	
	
	/**
	 * 验证邮箱格式
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/emailVerify")
	public String emailVerify(HttpServletRequest request){
		String email = request.getParameter("email");
		Map<String,Boolean> verifyM = (Map) request.getSession().getAttribute("registerVerify");
		if(new EmailVerifyUtil().stringVerify(email)){
			verifyM.put("email", true);
			request.getSession().setAttribute("registerVerify",verifyM);
			return "0";
		}
		verifyM.put("email", false);
		request.getSession().setAttribute("registerVerify",verifyM);
		return "1";
	}
	
	
	 /**
     * 生成验证码
     * @param request
     * @param response
     * @throws IOException
     * @ValidateCode.generateTextCode(验证码字符类型,验证码长度,需排除的特殊字符)
     * @ValidateCode.generateImageCode(文本验证码,图片宽度,图片高度,干扰线的条数,字符的高低位置是否随机,图片颜色,字体颜色,干扰线颜色)
     */

    @RequestMapping(value = "validateCode")
    public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control", "no-cache");
        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_LOWER, 4, null);
        request.getSession().setAttribute("validateCode", verifyCode);
        response.setContentType("image/jpeg");
        BufferedImage bim = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 5, true, Color.WHITE, Color.BLUE, null);
        ImageIO.write(bim, "JPEG", response.getOutputStream());
    }
    
    /**
     * 检查验证码是否正确
     * @return
     */
    @ResponseBody
    @RequestMapping("/codeVerify")
    public String codeVerify(HttpServletRequest request){
    	String verifyCode = request.getParameter("verifyCode");
    	HttpSession session = request.getSession();
    	Map<String,Boolean> verifyM = (Map) session.getAttribute("registerVerify");
    	
    	String code = (String) session.getAttribute("validateCode");
    	if(verifyCode.equals(code)){
    		verifyM.put("verifyCode", true);
    		session.setAttribute("registerVerify",verifyM);
    		return "0";
    	}
    	verifyM.put("verifyCode", false);
    	session.setAttribute("registerVerify",verifyM);
    	return "1";
    }
    
    /**
	 * 点击注册
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userRegister")
	public String userRegister(HttpServletRequest request){
		Map<String,Boolean> verifyM = (Map) request.getSession().getAttribute("registerVerify");
		if(!verifyM.get("username")){
			return "1";
		}else if (!verifyM.get("password1")) {
			return "2";
		}else if (!verifyM.get("password2")) {
			return "3";
		}else if (!verifyM.get("email")) {
			return "4";
		}else if (!verifyM.get("verifyCode")) {
			return "5";
		}else {
			String username = request.getParameter("username");
			String password1 = request.getParameter("password");
			String email = request.getParameter("email");
			String name = request.getParameter("Uname");
			String phone = request.getParameter("Uphone");
			String addr = request.getParameter("Uaddr");
			String sex = request.getParameter("Usex");
			User user = new User();
			user.setUsername(username);
			user.setPassword(password1);
			user.setEmail(email);
			user.setName(name);
			user.setPhone(phone);
			user.setAddr(addr);
			userService.insertUser(user);
			return "0";
		}
		
	}
}
